<?php
/* explain:		
 * Project:     
 * File:        qa.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Zone extends Common
{
	private $intLinkSign;
	private $iUserID;
	
	private $objLog;
	
	function Zone($usrid)
	{
		parent::Common();
		$this->intLinkSign = $this->getConnection("system");
		
		$this->objLog 		= new Logs($usrid);
		$this->iUserID 		= $usrid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM zones WHERE id = $id";
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* add method
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $name, $code2, $code3)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO zones SET name='$name', code2='$code2', code3='$code3'";
					
		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		$intDataID = mysql_insert_id($this->intLinkSign);
		if($rs)
			return $intDataID;
		else
			return false;
	}

	/* modify
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $name, $code2, $code3)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE zones SET name = '$name', code2='$code2', code3='$code3' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM zones WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
			
		$arrback = array();

		$sql = 'SELECT count(id) FROM zones'.$where;

		$rs  = mysql_query($sql, $this->intLinkSign);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = 'SELECT * FROM zones'.$where;
		else{
			$sql = 'SELECT * FROM zones'.$where." LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list by condition
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listFromCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM zones".$condition;

		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}

	public function getGroupList($countryid)
	{
		$sql 	= "SELECT id, name FROM zones WHERE countryID = $countryid";
		$rs 	= mysql_query($sql, $this->intLinkSign);
		
		$arrCache = array();
		if($rs){
			while ($arrData = mysql_fetch_array($rs)){
				$arrCache[$arrData['id']] = $arrData['name'];
			}
		}
		
		return $arrCache;
	}
}
?>
